<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head th:replace="common/common2 :: common_head(~{::title},~{},~{})">
    <title>分析日志查看</title>
</head>
<body>

<script type="text/javascript">
    Ext.onReady(function () {
        var changeApiParam = {
            GET_FXLZ_YWID_ADDRESS :GS.global.ctxPath + '/analyseFxrz/getAnalyseFxrzByYwid',
            GET_FXLZ_LS_YWID_ADDRESS:GS.global.ctxPath +'/analyseFxrz/getAnalyseFxrzLsByYwid'
        };
        /****枚举查找store*****/
        var analysisTypeStore = Ext.create('Ext.data.Store', {
            autoLoad: true,
            fields: ['type', 'name'],
            proxy: {
                type: 'ajax',
                api: {
                    read: GS.global.ctxPath + '/enum/getAnalysisTypeValues'
                },
                actionMethods: {
                    read: 'POST', // Store设置请求的方法，与Ajax请求有区别
                },
                /**设置读取方式*/
                reader: {
                    type: 'json'
                }
            }
        });


        /***mongo日志表**/
        var mongModel = Ext.create('Ext.data.Model', {
            fields: [
                {
                    type: 'hidden',
                    name: 'id' //id
                },
                {
                    type: 'string',
                    name: 'input' //输入
                },
                {
                    type: 'string',
                    name: 'output' //输出
                }
            ]
        });

        var mongoStore = Ext.create('Ext.data.Store', {
            model: mongModel,
            autoLoad: false,
            proxy: {
                type: 'ajax',
                api: {
                    read: GS.global.ctxPath +'/logMongo/getMongoLogById'
                },
                actionMethods: {
                    read: 'GET', // Store设置请求的方法，与Ajax请求有区别
                },
                /**设置读取方式*/
                reader: {
                    type: 'json',
                    rootProperty: 'data'
                }
            }
        });

        var mongoForm = Ext.create('Ext.form.Panel', {
            border: false,
            layout: {type: 'auto', align: 'right'},
            buttonAlign: 'center',
            defaultType: 'textfield',
            bodyStyle: {
                padding: '20px'
            },
            items: [
                {
                    xtype: 'container',
                    layout: 'hbox',
                    items: [
                        {
                            xtype: 'hiddenfield',
                            name: 'id',
                        }, {
                            xtype: 'hiddenfield',
                            name: 'actionType',
                        },
                        {
                            xtype: 'textareafield',
                            fieldLabel: '输入',
                            name: 'input',
                            flex: 10,
                            margin: '5 5 5 5',
                            allowBlank: false,
                            height: 140,
                            readOnly: 'true'
                        }
                    ]
                },
                {
                    xtype: 'container',
                    layout: 'hbox',
                    items: [
                        {
                            xtype: 'textareafield',
                            fieldLabel: '输出',
                            name: 'output',
                            flex: 10,
                            margin: '15 5 5 5',
                            allowBlank: false,
                            height: 140,
                            readOnly: 'true'
                        }
                    ]
                },
            ]
        });

        var profileWindow = Ext.create('Ext.Window', {
            title: 'mongo日志展示界面',
            width: 600,
            height: 450,
            resizable: false,
            modal: true,
            items: [mongoForm],
            maximizable: true,
            minimizable: true,
            closeAction: 'hide',//设置点击关闭按钮为隐藏,不设置，window不能重复利用
            buttons: [{
                xtype: 'button', text: '关闭', handler: function () {
                    mongoForm.getForm().reset();
                    profileWindow.hide();
                }
            }]
        });
        /***右侧日志表单**/
        var model = Ext.create("Ext.data.Model", {
            fields: [
                {
                    type:'string',
                    name:'ywid'
                },
                {
                    type:'string',
                    name:'xmmc'
                },
                {
                    type: 'string',
                    name: 'errorMsg'
                }, {
                    type: 'string',
                    name: 'bz'
                }, {
                    type: 'string',
                    name: 'logStartTime'
                }, {
                    type: 'string',
                    name: 'fxjg'
                },{
                    type: 'string',
                    name: 'mongoId'
                },{
                    type: 'string',
                    name: 'costTime'
                }
            ]
        });

        var store = Ext.create('Ext.data.Store',{
            model: model,
            autoLoad: false,
            proxy:{
                type: 'ajax',
                api:{
                    read: GS.global.ctxPath + '/analyseFxrz/getAnalyseFxrzByYwid'
                },
                actionMethods : {
                    read : 'POST' // Store设置请求的方法，与Ajax请求有区别
                },
                /**设置读取方式*/
                reader:{
                    type: 'json',
                    rootProperty: 'data.records',
                    totalProperty: 'data.total'
                }
            },
            listeners: {
            }
        });

        var grid = Ext.create('Ext.grid.Panel', {
            id:'fxrzGrid',
            region: 'center',
            store: store,
            columns: [
                {
                    xtype: 'rownumberer',
                    header: '行号',
                    renderer: function (value, metadata, record, rowIndex) {
                        return (rowIndex + 1);
                    },
                    width: 50,
                    align: 'center'
                },
                {
                    text: '分析类型',
                    width: 140,
                    dataIndex: 'fxlx',
                    renderer:function (value){
                        if(value){
                            var displayField = '';
                            var mapped;
                            analysisTypeStore.getData().items.forEach(function (e) {
                                if (value === e.data.type) {
                                    displayField = e.data.name;
                                    mapped = true;
                                }
                            });
                            if(mapped){
                                return Ext.String.format('{0}',displayField);
                            }else {
                                return value;
                            }
                        }else{
                            return "-";
                        }
                    }
                },
                {
                    text: '错误信息',
                    width: 250,
                    dataIndex: 'errorMsg',
                    renderer:function (value){
                        if(value){
                            return value;
                        }else{
                            return "无错误信息";
                        }
                    }
                },
                /*{
                    text: '映射后分析类型',
                    width: 130,
                    dataIndex: 'fxlxMapped',
                    renderer:function (value){
                        if(value){
                            return value;
                        }else{
                            return "-";
                        }
                    }
                }*/{
                    text: '分析时间',
                    width:150,
                    dataIndex: 'fxsj',
                    renderer:function (value) {
                        if(value) {
                            return Ext.Date.format(new Date(value), 'Y-m-d G:i:s');
                        }
                        return "-";
                    }
                },{
                    text: '分析消耗时间',
                    width: 100,
                    dataIndex: 'costTime',
                    renderer:function (value) {
                        if(value){
                            var newValue=value+"秒"
                            return newValue;
                        }
                        else{
                            return "-";
                        }
                    }
                },{
                    text: '分析结果',
                    width: 90,
                    dataIndex: 'fxjg',
                    renderer:function (value) {
                        if(value) {
                            if (value === 'true') {
                                return '成功';
                            }
                            else {
                                return "失败";
                            }
                        }else{
                            return "-";
                        }
                    }
                },{
                    text: '备注',
                    flex:10,
                    dataIndex: 'bz',
                    renderer:function (value) {
                        if(value) {
                            if(value.indexOf(",")!==-1){
                                var rs1=value.split(",")[1]==='1'?"成功":"失败";
                                var rs2=value.split(",")[2]==='1'?"成功":"失败";
                                return rs1+" "+rs2;
                            }else {
                                return "异常原因："+value;
                            }
                        }else{
                            return "-";
                        }
                    }
                }/**,{
                    text: 'mongoId',
                    flex:10,
                    dataIndex: 'MONGO_ID'
                }**/
            ],
            listeners: {
                cellclick: function (that,current,td,cellIndex,record,tr,rowIndex,e) {
                    showMongoLog();
                },
                cellcontextmenu:function (that,td,cellIndex,record,tr,rowIndex,e,eOpts){
                    e.preventDefault();
                    if(cellIndex == 2){
                        Ext.Msg.alert("错误信息",record.data.errorMsg===null?"该案卷无错误信息":""+record.data.errorMsg);
                    }
                }
        }
        });

        /***统计表单开始****/
        var storeTj = Ext.create('Ext.data.Store', {
            model: model,
            autoLoad: true,
            start: 0,
            limit: 5,
            pageSize: 5,
            fields:['dataType', 'success', 'total','tzhdlfxtime','xmmc','xzqhmc','ywid'],
            groupField: 'ywid',
            proxy:{
                type: 'ajax',
                api:{
                    read: GS.global.ctxPath +'/analyseFxrz/getAnalyseStastics'
                },
                actionMethods : {
                    read : 'POST' // Store设置请求的方法，与Ajax请求有区别
                },
                /**设置读取方式*/
                reader:{
                    type: 'json',
                    rootProperty: 'data.records',
                    totalProperty: 'data.total'
                }
            },
            listeners: {
                'beforeload': function (store, op, options) {
                    /**从头开始*/
                    var params = {
                        query_ywid:Ext.getCmp('query_ywid').getValue()
                    };
                    Ext.apply(store.proxy.extraParams, params);
                }
            },
            sorters:[{property:"dataType",direction:"DESC"}]
        });

        var gridTj = Ext.create('Ext.grid.Panel', {
            region: 'west',
            split:true,
            width: 530,
            store: storeTj,
            tbar: [
                {
                    xtype : 'textfield',
                    id:'query_ywid',
                    name:'query_ywid',
                    emptyText: '请输入业务id...',
                },{
                    xtype : 'button',
                    id : 'btnStuListQueryYwid',
                    text: '查询',
                    iconCls: 'Preview',
                    handler: function () {
                        storeTj.loadPage(1);
                    },
                }, {
                    xtype: 'button',
                    id: 'btnStuListResetYwid',
                    iconCls: 'Arrowrefresh',
                    text: '刷新',
                    handler: function () {
                        Ext.getCmp('query_ywid').reset();
                        storeTj.loadPage(1);
                        store.removeAll();
                    }
                }
            ],
            columns: [{
                text: '表单类型',
                dataIndex: 'dataType',
                width:90,
                renderer:function (value){
                    if(value === 'xz'){
                        return '分析日志表';
                    }else{
                        return "历史表";
                    }
                }
            }, {
                text: '成功次数',
                dataIndex: 'success',
                width:75
            },{
                text: '分析次数',
                dataIndex: 'total',
                width:75
            },{
                text: '地类分析时间',
                dataIndex: 'tzhdlfxtime',
                width:160,
                renderer:function (value){
                    if(value){
                        return Ext.Date.format(new Date(value), 'Y-m-d G:i:s');
                    }else{
                        return "-";
                    }
                }
            },{
                text: '项目名称',
                dataIndex: 'xmmc',
                flex:10,
                renderer:function (value){
                    if(value){
                        return value;
                    }else{
                        return "-";
                    }
                }
            }],
            features: [{
                ftype:'grouping',
                id: 'group',
                groupHeaderTpl: '业务id:{name}'
            }],
            renderTo: Ext.getBody(),
            dockedItems: [{
                id: 'pagingbar',
                xtype: 'pagingtoolbar',
                store: storeTj,
                dock: 'bottom'
            }],
            listeners:{
                cellclick:function (current,td,cellIndex,record,tr,rowIndex,e) {
                    var dataType = record.data.dataType;
                    var records = gridTj.store.data.items[rowIndex];
                    var ywid = records.data.ywid;
                    changeStoreParamAndApi(dataType,ywid);
                },
                cellcontextmenu:function (that,td,cellIndex,record,tr,rowIndex,e,eOpts){
                    e.preventDefault();
                    if(cellIndex == 4){
                        Ext.Msg.alert("项目名称",record.data.xmmc===null?"该项目名称为空":""+record.data.xmmc);
                    }
                }
            }
        });

        new Ext.Viewport({
            layout: 'border',
            title: ' Window',
            items: [{ region: 'north',title:'分析日志查看页面'},gridTj,grid]
        });

        //展示mongo窗口
        function showMongoLog() {
            var records = grid.selModel.getSelection();
            var selectedMongoId=records[0].data.mongoId;
            if(selectedMongoId === undefined || selectedMongoId ==="" || selectedMongoId === null){
                Ext.Msg.alert("提示","该案卷未保存mongo数据！");
            }else{
                mongoStore.getProxy().setExtraParam('mongoId',selectedMongoId);
                mongoStore.load(function callback() {
                    if(mongoStore.data.items.length>0) {
                        var mongoData = mongoStore.data.items[0].data;
                        mongoForm.getForm().setValues(mongoData);
                        profileWindow.show();
                    }else{
                        mongoForm.getForm().setValues(
                            {
                                "input":"未搜索到输入数据",
                                "output":"未搜索到输出数据"
                            }
                        );
                        profileWindow.show();
                    }
                });
            }
        }

        //改变stroe的地址和查询时传递的参数
        function changeStoreParamAndApi(dataType,ywid) {
            var params = {
                ywid: ywid
            };
            if(dataType === 'xz'){
                var apiParam={
                    read:changeApiParam.GET_FXLZ_YWID_ADDRESS
                }
            }
            if(dataType === 'ls'){
                var apiParam={
                    read:changeApiParam.GET_FXLZ_LS_YWID_ADDRESS
                };
            }
            Ext.apply(store.proxy.extraParams, params);
            Ext.apply(store.proxy.api, apiParam);
            grid.store.reload();
        }
    });
</script>
</body>
</html>